﻿using UnityEngine;

namespace Pisces
{
	
	public class SingletonRoot : MonoBehaviourSingleton<SingletonRoot>, InterfaceEntityLiftcycle
	{
		public virtual void onInit()
		{
		}

		public virtual void onLoaded()
		{
		}

		public virtual void onAwake()
		{
		}

		public virtual void onUpdate(float deltaTime)
		{
		}

		public virtual void onDestroy()
		{
		}
	}
	
	/**
	 * mono的单例
     * created by wangqiang
    **/
	public class MonoBehaviourSingleton<T> : MonoBehaviour where T : MonoBehaviour, InterfaceEntityLiftcycle
	{
		private static T singleton;
		
		public static T getSingleton()
		{
			if (singleton == null)
			{
				GameObject obj = new GameObject("MonoBehaviourSingleton_" + typeof(T).ToString());

				if (Application.isPlaying) 
				{
					GameObject.DontDestroyOnLoad (obj);
				}

				singleton = obj.AddComponent<T>();
				singleton.onInit();
				obj.transform.SetParent(SingletonRoot.getSingleton().transform);
			}
			return singleton;
		}
	}
	
}